package  
{

	import adobe.utils.CustomActions;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import Objetos.Carro;
	import Objetos.Obstaculos;
	/**
	 * ...
	 * @author Yago Jacques
	 */
	public class Main extends MovieClip
	{
		var carro:Carro 
		var level:Level
		var obstaculos:Obstaculos
		var AR_obstaculos:Array;
		var score: int = 0;
		var TX_score: TextField;
		var TX_scoreFormat: TextFormat;
		var UI_score: uint;
		
		protected var delayObstaculos:int;
		
		public static var _instance:Main;
		
		
		
		public function Main() 
		{
			stage.addEventListener(Event.ENTER_FRAME, update, false, 0, true);
			desenharTela();
			iniciarValores();
		}
		
		private function iniciarValores():void
		{
			delayObstaculos = 90;
			
		}
		private function desenharTela():void
		{
			AR_obstaculos = new Array();
			
			level = new Level();
			level.x = stage.stageWidth / 2;
			level.y = 600;
			addChild(level);
			
			
			_instance = this;
			carro = new Carro();
			carro.x = 250;
			carro.y = 500;
			stage.addChild(carro);
			
			TX_score = new TextField();
			TX_scoreFormat = new TextFormat();
			
			TX_score.type = TextFieldType.DYNAMIC
			TX_score.width = 200;
			TX_score.height = 50;
			TX_score.x = stage.stageWidth/2;
			TX_score.y = stage.stageHeight/2;
			TX_score.textColor = 000000;
			
			TX_scoreFormat.size = 20;
			TX_scoreFormat.align = "justify";
			TX_scoreFormat.bold = true;
			TX_scoreFormat.font = "Quartz MS";
			TX_score.defaultTextFormat = TX_scoreFormat;
			stage.addChild(TX_score)
		}
		
		public static function getInstance():Main
		{
			return _instance;
		}
		
		public function update(e:Event):void
		{
			if (carro != null)
			{
					carro.update();
					if (delayObstaculos <= 0)
				{
					obstaculos = new Obstaculos();
					obstaculos.x = Math.random()* 500;
					obstaculos.y = Math.random()* 100;
					obstaculos.scaleX = 0.2+Math.random();
					stage.addChild(obstaculos);
					AR_obstaculos.push(obstaculos);
					delayObstaculos = 25;
				}
			
			}
			
			level.update();
			colisao();
			limitarTela();
			
			
			for each (obstaculos in AR_obstaculos)
			{
				obstaculos.update();
			}
			
			delayObstaculos --;
			
			score ++;
			trace(score)		
			
			
		}
		
		private function colisao():void
		{
			for (var i:int = 0; i < AR_obstaculos.length; i++) 
			{
				if (carro != null && AR_obstaculos[i] != null)
				{
					if (carro.hitTestObject(AR_obstaculos[i]))
					{
						stage.removeChild(AR_obstaculos[i]);
						AR_obstaculos.splice(i, 1);
						stage.removeChild(carro);
						carro = null;
						trace("morreu... se fudeuuu!!!");
						stage.removeEventListener(Event.ENTER_FRAME, update);
					}
					
					else if (AR_obstaculos[i].y >= stage.stageHeight)
					{
						trace("S A I U     D A     S T A G E ");
						stage.removeChild(AR_obstaculos[i]);
						AR_obstaculos.splice(i, 1);
						
					}
				}
			}
		}
		
		
		
		private function limitarTela():void
		{
			if (carro != null)
			{
			if (carro.x >= 460)
			{
				carro.x = 460;
			}
			
			if (carro.x <= 0)
			{
				carro.x = 0;
			}
			
			if (carro.y >= 600)
			{
				carro.y = 600;
			}
			}
			
			
		}
		
	}

}
